//
//  MJTStatusOriginalView.swift
//  swiftWeiBo
//
//  Created by MJT on 16/7/13.
//  Copyright © 2016年 MJT. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
class MJTStatusOriginalView: UIView {
    // 记录原创微博底部的约束
    var originalViewBotton: Constraint?

    //c持有对应的显示数据的viewModel
    var statusViewModel: MJTStatusViewModel? {
        didSet {
            //判断URL是否为nil
            if let imageUrl1 = statusViewModel?.status?.user?.profile_image_url {
                //用户头像
                headImageView.sd_setImageWithURL(NSURL(string: imageUrl1), placeholderImage: UIImage(named:"avatar_default_big"))
            }
            //昵称
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name

            //会员等级
            mbrankImageView.image = statusViewModel?.mbrankImage
            //认证等级图片
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            //微博来源
            sourceLabel.text = statusViewModel?.sourceContent
            //距离现在发布时间
            timeLabel.text = statusViewModel?.timeStr
//            //微博内容
//            contentLabel.text = statusViewModel?.status?.text
            //设置富文本
            contentLabel.attributedText = statusViewModel?.origianlAttributedString
            //卸载约束
            originalViewBotton?.uninstall()
            //判断是否有配图,数据是否大于零
            if let count = statusViewModel?.status?.pic_urls?.count where count > 0{
                // 有配图,设置数据源,更新数据并显示
                pictureView.hidden = false
                pictureView.picURLs = statusViewModel?.status?.pic_urls
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBotton = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
            } else {
                //没配图,不显示,更新约束
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBotton = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
        }
    }
    //懒加载控件
    //头像
    private lazy var headImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证类型等级图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //微博昵称
    private lazy var screenNameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
    //会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 12)
    //来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 12)
    //微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
        label.numberOfLines = 0
        return label
    }()
    //原创微博配图
    private lazy var pictureView: MJTStatusPictureView1 = {
        let view = MJTStatusPictureView1()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
//        backgroundColor = UIColor.redColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        backgroundColor = UIColor.whiteColor()
        addSubview(headImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(10)
            make.leading.equalTo(self).offset(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(10)
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
        }
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(HomeCellMargin)
            make.top.equalTo(screenNameLabel)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HomeCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(HomeCellMargin)
            make.leading.equalTo(headImageView)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
        }
        self.snp_makeConstraints { (make) -> Void in
            self.originalViewBotton = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }


    }

}
